package com.example.back.service.impl;

import com.example.back.dto.req.DiseaseEditReq;
import com.example.back.dto.req.DiseaseListReq;
import com.example.back.dto.resp.DiseaseInfoRsp;
import com.example.back.dto.resp.DiseaseListResp;
import com.example.back.jpa.DiseaseRepository;
import com.example.back.mapper.DiseaseMapper;
import com.example.back.service.DiseaseService;
import com.example.common.entity.DiseaseInfo;
import com.example.common.entity.HospitalDepartment;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.example.common.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author wxy
 * @Date 2021/1/14 13:34
 * @Version 1.0
 */
@Service
public class DiseaseServiceImpl implements DiseaseService {

    @Resource
    private DiseaseMapper diseaseMapper;
    @Resource
    private DiseaseRepository diseaseRepository;

    /**
     * 疾病列表
     */
    @Override
    public PageInfo<DiseaseListResp> List(DiseaseListReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));
        //分页页码，默认1页
        int pageNumber = null == req.getPage() ? 1 : req.getPage();
        //分页条数，默认10条
        int pageSize = null == req.getLimit() ? 10 : req.getLimit();
        //疾病名称
        String name = req.getName();
        //状态
        Boolean disable = req.getDisable();

        PageInfo<DiseaseListResp> pageInfo = PageHelper
                .startPage(pageNumber, pageSize)
                .doSelectPageInfo(() -> diseaseMapper.findByDiseaseName(name, disable, req.getType()));

                pageInfo.getList().forEach(m -> m.setDTypeName(m.getDType() == 1 ? "中医病种" : m.getDType() == 2 ? "西医病种" : m.getDType() == 3 ? "中医辨证" : ""));
        return pageInfo;
    }

    /**
     * 启用、禁用
     */
    @Override
    @JaversAuditable
    public Object DoDisable(DiseaseEditReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));

        DiseaseInfo objs = diseaseRepository.findById(req.getDID()).orElse(null);
        if(objs == null){
            throw new ApiException(1, "系统参数错误");
        }

        objs.setDisable(!objs.getDisable());

        return diseaseRepository.save(objs);

    }

    /**
     * 详情
     */
    @Override
    public DiseaseInfoRsp Info(DiseaseEditReq req) {
        DiseaseInfoRsp diseaseInfoRsp = diseaseMapper.findByDcId(req.getDID());
        return diseaseInfoRsp;
    }

    /**
     * 编辑和添加疾病
     * @param req
     * @return
     */
    @Override
    @JaversAuditable
    public Object Save(DiseaseEditReq req) {
        DiseaseInfo diseaseInfoDb = diseaseRepository.findById(req.getID()).orElse(null);
        if (ObjectUtils.isEmpty(diseaseInfoDb)){
            DiseaseInfo diseaseInfo = new DiseaseInfo();
            diseaseInfo.setDiseaseName(req.getDiseaseName());
            diseaseInfo.setCode(req.getCode());
            diseaseInfo.setDType(req.getDType());
            diseaseInfo.setCreateTime(new Date());
            diseaseInfo.setDisable(false);
            diseaseRepository.save(diseaseInfo);
        }else {
            diseaseInfoDb.setCode(req.getCode());
            diseaseInfoDb.setDType(req.getDType());
            diseaseInfoDb.setDiseaseName(req.getDiseaseName());
            diseaseRepository.save(diseaseInfoDb);
        }

        return null;
    }
}
